package at.fhj.itm09.snake.logic;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import at.fhj.itm09.snake.views.SnakeView;

/**
 * @author Susanne Brandstätter - Susanne.Brandstaetter.ITM09@fh-joanneum.at
 * @author Katharina Biela - Katharina.Biela.ITM09@fh-joanneum.at
 *
 * Simple class that controls the buttons activity.
 * Because the SnakeView works with the DPAD KeyEvents, we have to simulate
 * such a keyevent with our buttons. 
 */

public class SnakeLogic implements OnClickListener {

	private SnakeView mSnakeView;
	private ImageButton btnDown;
	private ImageButton btnUp;
	private ImageButton btnRight;
	private ImageButton btnLeft;
	private KeyEvent keyEventDown;
	private KeyEvent keyEventUp;
	private KeyEvent keyEventLeft;
	private KeyEvent keyEventRight;
	
	private final int ACTION_DWON = 0;
	
	public SnakeLogic(SnakeView snakeView, ImageButton btnDown, ImageButton btnUp,
			ImageButton btnLeft, ImageButton btnRight){
		this.mSnakeView = snakeView;
		this.btnDown = btnDown;
		this.btnUp = btnUp;
		this.btnRight = btnRight;
		this.btnLeft = btnLeft;
		
		btnDown.setOnClickListener(this);
		btnUp.setOnClickListener(this);
		btnLeft.setOnClickListener(this);
		btnRight.setOnClickListener(this);
		
		//We load all keyevents we need. 
		keyEventDown = new KeyEvent(ACTION_DWON, KeyEvent.KEYCODE_DPAD_DOWN);
		keyEventUp = new KeyEvent(ACTION_DWON, KeyEvent.KEYCODE_DPAD_UP);
		keyEventLeft = new KeyEvent(ACTION_DWON, KeyEvent.KEYCODE_DPAD_LEFT);
		keyEventRight = new KeyEvent(ACTION_DWON, KeyEvent.KEYCODE_DPAD_RIGHT);
	}
	

	@Override
	public void onClick(View v) {
		/*
		 * Now we are going simulate the keys on a dpad
		 */
		if(btnDown.isPressed()){
			mSnakeView.onKeyDown(KeyEvent.KEYCODE_DPAD_DOWN, keyEventDown);
		}
		if(btnUp.isPressed()){
			mSnakeView.onKeyDown(KeyEvent.KEYCODE_DPAD_UP, keyEventUp);
		}
		if(btnLeft.isPressed()){
			mSnakeView.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, keyEventLeft);
		}
		if(btnRight.isPressed()){
			mSnakeView.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, keyEventRight);
		}
	}
	
}
